<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/jquery.min.js"></script>
    <script src="../js/mock-min.js"></script>
    <script src="../js/mock.js"></script>
</head>
<body>
    账号<input type="text"  id="acount">
    密码<input type="password" id="password" >
    姓名<input type="text"  id="name">
    年龄<input type="number" id="age" >
    性别
    <select name="" id="sex">
        <option value="0">男</option>
        <option value="1">女</option>
    </select>
    <button onclick="register()">注册</button>
    <button onclick="login()">登录</button>
    <script>
        class User{
            constructor(id,username,password,name,age,sex,newtime) {
                this.id=id
                this.username=username
                this.password=password
                this.name=name
                this.age=age
                this.sex=sex
                this.newtime=newtime
            }
        }
        const login=()=>{
            window.location.href='./login.html'
        }
        function register(){
            var valueone=document.getElementById('acount').value
            var valuetwo=document.getElementById('password').value
            var name=document.getElementById('name').value
            var age=document.getElementById('age').value
            var leilist=document.getElementById('sex').options
            let user=new User()
            for (const key in leilist) {
                    if(leilist[key].selected==true){
                        user.sex=leilist[key].innerText
                    }
            }
            user.username=valueone
            user.password=valuetwo
            user.name=name
            user.age=age
            let accountRegex = /^[a-zA-Z0-9]{5,}$/;
            let passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{6,}$/;
            let nameRegex = /^[\u4e00-\u9fa5]+$/;
            let isValidAccount = accountRegex.test(valueone);
            let isValidPassword = passwordRegex.test(valuetwo);
            let isValidName = nameRegex.test(name);
            if(!isValidAccount){
                return alert('账号需要至少5位字母或数字')
            }
            if(!isValidPassword){
                return alert('密码需要至少6位，且必须包含大小写字母和数字')
            }
            if(!isValidName){
                return alert('姓名必须为中文')
            }
            var date=new Date();
            user.newtime=date
            if(valueone==''||valuetwo==''){
                return alert('不能为空')
            }
            $.ajax({
                type:'post',
                url:'/js/useradd',
                data:JSON.stringify(user),
                success:function(data){
                }
            })

            // login()
        }

    </script>
</body>
</html>